package com.wms.controller;

import com.mes.entity.User;
import com.wms.entity.Inventory;
import com.wms.service.InventoryService;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.page.Message;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存盘点
 */
@Controller
@RequestMapping("inventory")
public class InventoryController extends BaseCtrl {
	@Autowired
	private InventoryService inventoryService;
	
	@ResponseBody
	@Transactional
	@RequestMapping(value="submit",method= RequestMethod.POST)
	public Message submit(HttpServletRequest request,
						  @RequestParam(value = "locatorCode") String locatorCode,
                          @RequestParam(value = "pid") String pid,
                          @RequestParam(value = "qty") String qty,
						  @RequestParam(value = "kcqty") String kcqty) throws Exception {
		
		Message message = new Message();
		boolean bOk = true;

		Map<String, Object> inventoryPlans = inventoryService.getInventoryPlan(locatorCode);
		if (inventoryPlans == null) {
			message.setErrorMessage("无盘点计划");
			return message;
		} else if (!"CREATE".equals(inventoryPlans.get("STATUS"))) {
			message.setErrorMessage("盘点计划已完成！");
			return message;
		}

		String loctaId = (String)inventoryPlans.get("LOCATORID");
		String wareId = (String)inventoryPlans.get("WAREHOUSEID");

		Inventory inventory = new Inventory();
		inventory.setInventoryPlanId((String) inventoryPlans.get("ID"));
		inventory.setWarehouseId(wareId);
		inventory.setLocatorId(loctaId);
		inventory.setId(StringUtil.getUUID());
		inventory.setQty(Double.parseDouble(kcqty));//库存数量
		inventory.setQtyOk(Double.parseDouble(qty));
		inventory.setProductId(pid);
		User user = this.getLoginUser(request);
		inventoryService.addInventory(inventory, user);
		if (bOk) {
			message.setOkMessage("提交成功");
		} else {
			message.setErrorMessage("请求失败");
		}
		return message;
	}

	/**
	 * 盘点录入
	 * codeLocator 库位表M_Locator中的Value
	 */
	@ResponseBody
	@RequestMapping(value="serch",method= RequestMethod.POST)
	public Message search(HttpServletRequest request, String value) {
		Message message = new Message();
		Map<String, Object> inventoryPlans = inventoryService.getInventoryPlan(value);
		if (inventoryPlans == null) {
			message.setErrorMessage("无盘点计划");
			return message;
		} else if (!"CREATE".equals(inventoryPlans.get("STATUS"))) {
			message.setErrorMessage("盘点计划已完成！");
			return message;
		}
		
		List<Map<String, Object>> locatorProducts = inventoryService.getLocatorProductQty(value);
		if (locatorProducts == null || locatorProducts.isEmpty()) {
			message.setErrorMessage("此货位物料为空！");
			return message;
		}
		
		List<Map<String, Object>> lData = new ArrayList<Map<String, Object>>();
		//获取仓库下所有物料库存信息
		for (Map<String, Object> m : locatorProducts) {
			Map dOpData = new HashMap<String, Object>();
			dOpData.put("PID", m.get("M_PRODUCT_ID"));
			dOpData.put("PRODUCTNAME", m.get("NAME"));
			dOpData.put("UNAME", m.get("UNAME"));
			dOpData.put("KCQTY", m.get("QTY"));
			dOpData.put("VAL", m.get("VAL"));
			lData.add(dOpData);
		}
		message.setOkMessage("ok");
		message.put("locatorId", inventoryPlans.get("LOCATORID"));
		message.put("wareHouseId", inventoryPlans.get("WAREHOUSEID"));
		message.setPageList(lData);
		return message;
	}
}
